#!/usr/bin/Rscript
##########################################################################################
# Issue Ownership and Agenda Setting in the 2019 Swiss National Elections
##########################################################################################
# Description:
# Script for Figure A1
##########################################################################################
# Content
##########################################################################################
# 1) Dependencies
# 2) Startup
# 3) Load Data
# 4) Transform Data
# 5) Figure A1
##########################################################################################
# 1) Dependencies
##########################################################################################
library(dplyr)
library(tidyverse)
library(data.table)
library(readr)
library(lubridate)
library(ggplot2)
library(ggExtra)
library(ggeffects)
library(ggrepel)
library(ggforce)
library(igraph)
library(graphlayouts)
library(ggraph)
library(scales)
library(purrr)
library(magrittr)
library(cowplot)
library(sysfonts)
##########################################################################################
# 2) Startup
##########################################################################################
rm(list=ls())
# - set dir
args = commandArgs()

scriptName = args[substr(args,1,7) == '--file=']

if (length(scriptName) == 0) {
    scriptName <- rstudioapi::getSourceEditorContext()$path
} else {
    scriptName <- substr(scriptName, 8, nchar(scriptName))
}

pathName = substr(
    scriptName, 
    1, 
    nchar(scriptName) - nchar(strsplit(scriptName, '.*[/|\\]')[[1]][2])
)


# - load fonts used in plots
sysfonts::font_add_google("Montserrat", "Montserrat")
sysfonts::font_add_google("Roboto", "Roboto")

# - load ddl theme
suppressMessages(suppressWarnings(source('../ggplot_theme_ddl.R')))
# - pre-setup for figure 1
ddl_theme_2 <- theme(legend.position = "bottom", legend.title = element_blank(), 
                     strip.background = element_blank(), strip.text = element_text(color = "black"),
                     axis.text.x = element_text(angle = 0, hjust = 0.5, vjust = 0.0, size = 16, color = "black"),  
                     axis.text.y = element_text(hjust=0, size = 16, color = "black"),
                     axis.ticks.y = element_blank(),
                     strip.text.x = element_text(size = 16, color = "black"),
                     axis.title = element_text(size = 16, color = "black"),
                     legend.text = element_text(size = 16, color = "black"),
                     plot.title = element_blank(),
                     plot.margin = unit(c(.5,.5,.5,.5), "cm"),
                     legend.key.size = unit(1.5,"line"),
                     legend.key = element_blank(),
                     axis.line.x = element_line(color="black", size = .5),
                     axis.line.y = element_line(color="black", size = .5),
                     panel.spacing.y = unit(2, "lines"),
                     panel.spacing.x = unit(.8, "lines")) 

# - set directory
setwd(pathName)
parent_path <- getwd()

dat_all <- read.csv("../data/parlgov_2021_elections.csv")

dat_ch <- filter(dat_all, country_name == "Switzerland")

table(dat_ch$party_name_english)

parties_select <- c(
    "Catholic Conservative / Christian Democratic Peoples Party",
    "Green Liberal Party",
    "Greens",
    "Radical Democratic Party",
    "Swiss People's Party",
    "Social Democratic Party of Switzerland"
)

dat_ch_select <- dat_ch %>% 
    mutate(election_date = as.Date(election_date)) %>% 
    filter(party_name_english %in% parties_select) %>% 
    filter(election_date > as.Date("1985-01-01")) %>% 
    mutate(party_name_english = ifelse(str_detect(party_name_english, "Christian Democratic Peoples Party"),
                                       "Christian Democratic Party", party_name_english)) %>%
    mutate(party_name_english = ifelse(str_detect(party_name_english, "Radical Democratic Party"),
                                       "Liberal Party", party_name_english)) %>% 
    mutate(party_name_abbr = case_when(
        str_detect(party_name_english, "Christian Democratic") ~ "CVP",
        str_detect(party_name_english, "Green Liberal Party") ~ "GLP",
        str_detect(party_name_english, "Social Democratic Party of Switzerland") ~ "SP",
        str_detect(party_name_english, "Liberal Party") ~ "FDP",
        str_detect(party_name_english, "Swiss People") ~ "SVP",
        
        
    )) %>% 
    mutate(party_plot = paste0(party_name_english, " (", party_name_abbr, ")")) %>% 
    mutate(party_plot = ifelse(party_plot == "Greens (NA)", "Green Party of Switzerland (Greens)", party_plot))



colours <- c("#EF7D00",
             "#A6CF42",
             "#84B414",
             "#0E52A0",
             "#E53136",
             "#009F4F")

min(dat_ch_select$election_date)

ggplot(dat_ch_select, aes(x = election_date, y = vote_share / 100,
                          colour = party_plot)) +
    geom_line() +
    geom_point(size = 3) +
    scale_y_continuous(labels = scales::percent_format(accuracy = 1),
                       limits = c(0, 0.3)) +
    facet_wrap(~party_plot, nrow = 3) +
    scale_colour_manual(values = colours) +
    labs(x = "Election date", y = "Vote share") +
    theme_bw() +
    theme(legend.position = "none") +
    ddl_theme_2 +
    theme(legend.position = "none", legend.title = element_text(size = 16, color = "black"), 
          strip.background = element_blank(), strip.text = element_text(color = "black"),
          axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1, size = 12, color = "black"),  
          axis.text.y = element_text(hjust=.5, size = 12, color = "black"),
          strip.text.x = element_text(size = 12, color = "black"),
          axis.title = element_text(size = 14, color = "black"),
          plot.title = element_blank(),
          legend.text = element_text(size = 14, color = "black"),
          legend.key = element_blank(),
          plot.margin = unit(c(.5,.5,.5,.5), "cm"),
          legend.key.size = unit(1.5,"line"),
          axis.line.x = element_line(color="black", size = .5),
          axis.line.y = element_line(color="black", size = .5),
          panel.grid.major = element_blank(),
          panel.grid.minor = element_blank(),
          panel.border = element_blank(),
          panel.background = element_blank())
ggsave("../images/figure_A02.pdf", 
       width = 9, height = 7)

